using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Core;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Input;

namespace SpaceEdit
{
    public partial class OldEditor : Form, IController
    {
        protected Game _game;
        public Game Game
        {
            get { return _game; }
            set { _game = value; }
        }

        public float Zoom
        {
            get { return zoom; }
            set { zoom = Zoom; }
        }

        public void Render()
        {
            UpdateForm();
        }

        private GameObject DragObject;
        private Surface Renderer;
        private float zoom = 1.0f;

        public OldEditor()
        {
            InitializeComponent();

            ScreenSetup();

            if (_game == null) _game = new Game();
            _game.AddController(this);

            propertyGrid1.SelectedObjects = Game.Objects.ToArray();

            SdlDotNet.Core.Events.TargetFps = 45;
            SdlDotNet.Core.Events.VideoResize += Events_Resize;
            SdlDotNet.Core.Events.Tick += Events_Tick;
            SdlDotNet.Core.Events.Quit += Events_Quit;

            SdlDotNet.Core.Events.KeyboardDown += Events_KeyboardDown;
        }

        public OldEditor(Game game) : this()
        {
            _game = game;
        }

        private static void Events_KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape)
                Application.Exit();
        }

        private void ScreenSetup()
        {
            Renderer = new Surface(surfaceControl1.Width, surfaceControl1.Height);
        }

        private void UpdateForm()
        {
            if (Game.Ticks % 2 > 0)
            {
                Renderer.Fill(Color.Black);
                foreach (GameObject obj in Game.Objects)
                {
                    Renderer.Blit(obj.Render());
                }

                surfaceControl1.Blit(Renderer);
            }
        }

        private void Events_Tick(object Sender, TickEventArgs e)
        {
            Game.Update(e.Tick);
        }

        private void Events_Resize(object Sender, VideoResizeEventArgs e)
        {
            Renderer = new Surface(e.Width, e.Height);
        }

        private static void Events_Quit(object sender, QuitEventArgs e)
        {
            SdlDotNet.Core.Events.QuitApplication();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(SdlDotNet.Core.Events.Run);
            thread.IsBackground = true;
            thread.Name = "SDL.NET";
            thread.Priority = ThreadPriority.Normal;
            thread.Start();
        }

        private void surfaceControl1_MouseClick(object sender, MouseEventArgs e)
        {
            propertyGrid1.SelectedObject = null;

            foreach (GameObject obj in Game.Objects)
            {
                if (obj.Hit(e.Location))
                    propertyGrid1.SelectedObject = obj;
            }
        }

        private void surfaceControl1_MouseDown(object sender, MouseEventArgs e)
        {
            DragObject = null;
            foreach (GameObject obj in Game.Objects)
            {
                if (obj.Hit(e.Location))
                {
                    propertyGrid1.SelectedObject = obj;
                    DragObject = obj;
                }
            }
        }

        private void surfaceControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if (DragObject != null)
                DragObject = null;
        }

        private void surfaceControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (DragObject != null)
                DragObject.Position = e.Location;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Game.AddObject(new Core.GameObjects.Player(10, 10));
        }
    }
}